import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path/path.dart';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../helper/channelHelper.dart';
import '../../model/skddl2.dart';
import '../../model/space_item.dart';
import '../../model/stationcheck.dart';
import '../helper/upload_helper.dart';

import '../model/jxgzp.dart';
import '../model/skddl.dart';
import '../model/ftpfile.dart';
import 'Helper.dart';

class DownloadHelper {
  //static final url = "http://47.95.205.213:12345/upload";
  static final url = "http://47.108.62.6:12346/download?id=";
  static final url2 = "http://47.95.205.213:22350/download?id=";
  static final url3 = "http://47.95.205.213:22356/download?id=";
  static String localPath="";


  static Future<String?> DownLoad(BuildContext context, FtpFile ftp) async {
    var pbPermission = await _checkPermission(context);
       var savedDir = Helper.DirPath;
   // final savedDir = Directory(localPath);
    bool hasExisted = await savedDir.exists();
    if (!hasExisted) {
      savedDir.create();
    }



    final taskId = await FlutterDownloader.enqueue(
      url: url + ftp.Guid,
      savedDir: savedDir.absolute.path,
      showNotification:
          true, // show download progress in status bar (for Android)
      openFileFromNotification:
          true, // click on notification to open downloaded file (for Android)
      fileName: ftp.FileName,
    );

    return taskId;
  }

static Future<String?> DownLoad2(BuildContext context, SpaceItem ftp) async {
    var pbPermission = await _checkPermission(context);
   // localPath =await ChannelHelper.getFileDir();
    //localPath =await Helper.DirPath
    var savedDir = Helper.DirPath;
   // final savedDir = Directory(localPath);
    bool hasExisted = await savedDir.exists();
    if (!hasExisted) {
      savedDir.create();
    }
  print(url+ftp.Guid);
    final taskId = await FlutterDownloader.enqueue(
      url: url + ftp.Guid,
      savedDir: savedDir.absolute.path,
      showNotification:
          true, // show download progress in status bar (for Android)
      openFileFromNotification:
          true, // click on notification to open downloaded file (for Android)
      fileName: ftp.FileName,
    );

    return taskId;
  }


  static Future<String?> DownLoadByGuid(BuildContext context, String guid, String houzui) async {
   var pbPermission = await _checkPermission(context);
       var savedDir = Helper.DirPath;

  
    bool hasExisted = await savedDir.exists();
    if (!hasExisted) {
      savedDir.create();
    }

    final taskId = await FlutterDownloader.enqueue(
      url: url + guid,
      savedDir: savedDir.absolute.path,
      showNotification:
          true, // show download progress in status bar (for Android)
      openFileFromNotification:
          true, // click on notification to open downloaded file (for Android)
      fileName:guid+houzui,
    );

    return taskId;
  }



  static Future<DownloadTask?> GetDownLoadTask(String fileId) async {
    final tasks = await FlutterDownloader.loadTasks();
    var xx = tasks!.where((p) => p.url == (url + fileId));
    if (xx.length > 0) {
      return xx.first;
    }else {

      return null;
    }
  }

  static Future<String?> GetLocalFile(String fileId) async {
    var u1 = await UploadHelper.getByGuid(fileId);
    if (u1 != null) {
      return u1.filePath;
    }

    var d1 = await GetDownLoadTask(fileId);

    
    if(d1==null)
    return "";

    if (d1.status == DownloadTaskStatus.complete) {
      return ("${d1.savedDir}//${d1.filename}");
    }

    return "";
  }

  static Future<String?> DownLoadPDF(BuildContext context,String url,String title) async {

    var pbPermission = await _checkPermission(context);
       var savedDir = Helper.DirPath;

    bool hasExisted = await savedDir.exists();
    if (!hasExisted) {
      savedDir.create();
    }

    final taskId = await FlutterDownloader.enqueue(
      url: url,
      savedDir: savedDir.absolute.path,
      showNotification:
          true, // show download progress in status bar (for Android)
      openFileFromNotification:
          true, // click on notification to open downloaded file (for Android)
      fileName:
          title + DateTime.now().millisecondsSinceEpoch.toString() + ".pdf",
    );
    return taskId;
  }

  static Future<String?> DownLoadSKDDL2(BuildContext context, SKDDL2 s1) async {
     var pbPermission = await _checkPermission(context);
       var savedDir = Helper.DirPath;
    bool hasExisted = await savedDir.exists();
    if (!hasExisted) {
      savedDir.create();
    }

    final taskId = await FlutterDownloader.enqueue(
      url: url2 + s1.ID.toString(),
      savedDir: savedDir.absolute.path,
      showNotification:
          true, // show download progress in status bar (for Android)
      openFileFromNotification:
          true, // click on notification to open downloaded file (for Android)
      fileName:
          s1.Title + DateTime.now().millisecondsSinceEpoch.toString() + ".pdf",
    );

    return taskId;
  }

  static Future<String?> DownLoadJXGZP(BuildContext context, JXGZP s1) async {
      var pbPermission = await _checkPermission(context);
       var savedDir = Helper.DirPath;
    bool hasExisted = await savedDir.exists();
    if (!hasExisted) {
      savedDir.create();
    }

    final taskId = await FlutterDownloader.enqueue(
      url: url2 + s1.ID.toString(),
      savedDir: savedDir.absolute.path,
      showNotification:
          true, // show download progress in status bar (for Android)
      openFileFromNotification:
          true, // click on notification to open downloaded file (for Android)
      fileName:
          s1.Title + DateTime.now().millisecondsSinceEpoch.toString() + ".pdf",
    );

    return taskId;
  }


static Future<String?> DownLoadStationCheck(BuildContext context, StationCheck s1) async {
     var pbPermission = await _checkPermission(context);
       var savedDir = Helper.DirPath;
    bool hasExisted = await savedDir.exists();
    if (!hasExisted) {
      savedDir.create();
    }

    final taskId = await FlutterDownloader.enqueue(
      url: url3 + s1.ID.toString(),
      savedDir: savedDir.absolute.path,
      showNotification:
          true, // show download progress in status bar (for Android)
      openFileFromNotification:
          true, // click on notification to open downloaded file (for Android)
      fileName:
          s1.StationName +"_"+Helper.DateTimeFormat(s1.CreateTime)+"_"+ DateTime.now().millisecondsSinceEpoch.toString() + ".pdf",
    );

    return taskId;
  }

  

  static Future<bool> _checkPermission(BuildContext context) async {
   
         await [
      Permission.storage,
    ].request();

     Map<Permission, PermissionStatus> statuses =
        await [Permission.storage].request();
    print(statuses[Permission.storage]);
    var status = await Permission.storage.status;
    if (status.isGranted) {
      return true;
    } else {
      return false;
    }
  }
}
